package com.torry.edss.infras.util;

import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;

/**
 * @author torry.wang
 */
public class DateUtil {
    private static final String DATE_PATTERN = "yyyy-MM-dd";
    private static final String DATETIME_PATTERN = "yyyy-MM-dd HH:mm:ss";

    public static String formatDate(LocalDate date) {
        return DateTimeFormatter.ofPattern(DATE_PATTERN).format(date);
    }

    public static String formatDate(LocalDate date, String pattern) {
        return DateTimeFormatter.ofPattern(pattern).format(date);
    }
    public static String formatDate(LocalDateTime dateTime) {
        return DateTimeFormatter.ofPattern(DATE_PATTERN).format(dateTime);
    }

    public static LocalDate parseDate(String date) {
        return LocalDate.parse(date, DateTimeFormatter.ofPattern(DATE_PATTERN));
    }
    public static LocalDate parseDate(String date, String pattern) {
        return LocalDate.parse(date, DateTimeFormatter.ofPattern(pattern));
    }

    public static String formatDateTime(LocalDateTime dateTime) {
        return DateTimeFormatter.ofPattern(DATETIME_PATTERN).format(dateTime);
    }

    public static LocalDateTime parseDateTime(String dateTime) {
        return LocalDateTime.parse(dateTime, DateTimeFormatter.ofPattern(DATETIME_PATTERN));
    }

    public static LocalDateTime convertToDateTime(LocalDate date, String time) {
        String day = DateUtil.formatDate(date);
        return DateUtil.parseDateTime(day + " " + time);
    }

    public static LocalDateTime convertToDateTime(LocalDate date) {
        String day = DateUtil.formatDate(date);
        return DateUtil.parseDateTime(day + " 00:00:00");
    }

    public static LocalDate convertToDate(LocalDateTime date) {
        return parseDate(DateUtil.formatDate(date));
    }
    public static LocalDateTime concat(LocalDate date, String time) {
        String day = DateUtil.formatDate(date);
        return DateUtil.parseDateTime(day + " " + time);
    }

    public static LocalDateTime concat(LocalDateTime dateTime, String time) {
        String day = DateUtil.formatDate(dateTime);
        return DateUtil.parseDateTime(day + " " + time);
    }

    public static String getNowDate() {
        return formatDate(LocalDate.now());
    }

}
